package com.lf.web.action.system;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.lf.web.service.ResponseCode;
import com.lf.web.service.ResponseInfo;
import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.json.JSONObject;

import com.lf.web.service.system.config.Config;
import com.lf.data.check.ConfigCheck;
import com.lf.web.service.system.config.ConfigDAO;
import com.opensymphony.xwork2.ActionSupport;

public class ConfigService extends ActionSupport implements
        ServletResponseAware {

    /**
     *
     */
    private static final long serialVersionUID = -2958732568425437005L;
    private HttpServletResponse res;
    private static final Logger LOGGER = Logger.getLogger(ConfigService.class);
    private ConfigCheck check = new ConfigCheck();
    private ConfigDAO configdao;
    private int pagenumber = 1;
    private int pagecount = 20;

    private String updateName = "";
    private String updateValue = "";

    private Map<String, Object> rslist = null;
    private Config config;
    public String id = "";
    public String name = "";
    public String description = "";
    public String property = "";
    public String value = "";

    public void setConfigdao(ConfigDAO configdao) {
        this.configdao = configdao;
    }

    public void setPagenumber(int pagenumber) {
        this.pagenumber = pagenumber;
    }

    public void setPagecount(int pagecount) {
        this.pagecount = pagecount;
    }

    public void setUpdatename(String updateName) {
        this.updateName = updateName;
    }

    public void setUpdatevalue(String updateValue) {
        this.updateValue = updateValue;
    }

    @Override
    public void setServletResponse(HttpServletResponse res) {
        this.res = res;
    }

    public void setId(String id) {
        if (check.checkId(id))
            this.id = id;
        else
            this.id = "";
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setProperty(String property) {
        this.property = property;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String execute() {
        config = new Config();
        config.put(config.NAME, 1);
        config.put(config.DESCRIPT, 1);
        //rslist = configdao.finds(null, config, pagenumber, pagecount, "config");
        return SUCCESS;
    }

    public String add() {
        return "add";
    }

    public void addAction() throws IOException {
        if (check.checkName(name) && check.checkDescription(description)) {
            JSONObject json = new JSONObject();
            config = new Config();
            id = config.setId();
            config.put(config.NAME, name);
            config.put(config.DESCRIPT, description);
            config.put(config.OPTION, new HashMap<>());
            configdao.insert(config);
            json.put("code", ResponseCode.FINISH);
            json.put("id", id);
            res.getWriter().write(json.toString());
        } else {
            res.sendError(404);
            LOGGER.info(String.format("非法添加配置：Name=%s,Description=%s", name,
                    description));
        }
    }

    public void propertyAdd() throws IOException {
        if (!id.equals("") && check.checkProperty(property)
                && check.checkValue(value)) {
            config = new Config();
            JSONObject json = new JSONObject();
            //int rs = configdao.update(id, config.OPTION + "." + property, value);
            //json.put("code", rs);
            json.put("msg", "添加成功！");
            res.getWriter().write(json.toString());
        } else {
            res.sendError(404);
            LOGGER.info(String.format("非法添加属性：ID=%s,Property=%s,Value=%s", id,
                    property, value));
        }
    }

    public String detail() throws IOException {
        if (id.equals("")) {
            res.sendError(404);
            return NONE;
        } else {
            config = new Config(configdao.findById(id));
        }
        return "detail";
    }

    public void save() throws IOException {
        try {
            if (check.checkUpdateName(updateName) && !id.equals("")
                    && check.doCheck(check, updateName, updateValue)) {
                int rs = 0;//configdao.update(id, updateName, updateValue);
                JSONObject json = new JSONObject();
                json.put("code", rs);
                if (rs == 1) {
                    json.put("msg", "保存成功！");
                } else {
                    json.put("msg", "保存失败！");
                }
                res.getWriter().write(json.toString());
            } else {
                LOGGER.error("非法参数：id=" + id + ",updateName=" + updateName
                        + ",updateValue=" + updateValue);
                res.sendError(404);
            }
        } catch (Exception e) {
            LOGGER.error("异常参数：id=" + id + ",updateName=" + updateName
                    + ",updateValue=" + updateValue + "：" + e.getMessage());
            res.sendError(404);
        }
    }

    public void delete() throws IOException {
        if (!id.equals("")) {
            configdao.removeById(id);
            ResponseInfo ri = new ResponseInfo();
            ri.setCode(ResponseCode.FINISH);
            res.getWriter().write(ri.toString());
        } else {
            LOGGER.error("非法删除配置：id=" + id);
            res.sendError(404);
        }
    }

    public void propertyDelete() throws IOException {
        if (!id.equals("") && check.checkProperty(property)) {
            config = new Config();
            config.put(config.OPTION + "." + property, 1);
            JSONObject json = new JSONObject();
            //int rs = configdao.updateUnset(id, config);
            //json.put("code", rs);
            json.put("msg", "删除成功！");
            res.getWriter().write(json.toString());
        } else {
            res.sendError(404);
            LOGGER.info(String.format("非法删除属性：Id=%s,Property=%s", id, property));
        }
    }

    public Map<String, Object> getConfig() {
        return config;
    }

    public Map<String, Object> getRslist() {
        return rslist;
    }

    public ConfigCheck getCheck() {
        return check;
    }

}
